/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkstateGridB/MkStateGridB.h"
#include <MkUtil/MkLog.h>
#include "MkEvent/MkEventPool.h"
#include <signal.h>
#include "MkEvent/MkTimer.h"

MkString AlarmSubscribeId = "111";
MkString StatusSubscribeId = "222";
MkString VideoPhoneSession = "555";
MkString RealplaySession = "666";
MkString BroadcastSession = "777";
Uint32 MessageReqestCallback(const CMkStateGridBRequestBase* pRequest, const MkStateGridBResponseCallback& ResCb)
{
    switch (pRequest->GetMsgType()) {
    case MkStateGridBSubscribeNotify:
        MkDebugLog("recv evnet notify\n");
        ResCb(NoneError, nullptr);
        break;
    case MkStateGridBMediaShutDown:
        MkDebugLog("recv remote media shut down\n");
        ResCb(NoneError, nullptr);
        break;
    case MkStateGridBRegister:
        MkDebugLog("recv register\n");
        ResCb(NoneError, nullptr);
    default:
        break;
    }
    return NoneError;
}

CMkTimer     g_Timer;
int main(int argc, char ** argv)
{

#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;

    Pool.Start();
    g_Timer.StartTimerThread();
    CMkStateGridB UpSvr(&Pool, &g_Timer);
    MkStateGridBStartParam Param;
    Param.LocalHost = "0.0.0.0";
    Param.LocalPort = 5060;
    Param.MappingHost = "";
    Param.MappingPort = 0;
    Param.RegisterExpires = 3600;
    Param.SubscribeExpires = 3600;
    Param.SipId = "34020000002000000001";
    Param.SipRealm = "3402000000";
    UpSvr.Start(Param, [](const CMkStateGridBRequestBase* pRequest, const MkStateGridBResponseCallback& ResCb) {
        return MessageReqestCallback(pRequest, ResCb);
    });

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            g_Timer.LoopBreak();
        }
    });// set exit  signal

    MkMap<MkString, Uint32> MapSubId;
    MkVectorString VecSubId;

    MapSubId["111"] = 111;
    MapSubId["222"] = 222;
    VecSubId.push_back("12");
    VecSubId.push_back("23");
    char GetC;
    while (GetC = getchar()) {
        BOOL bQuit = FALSE;
        MkDebugLog("get char:%c\n", GetC);
        switch (GetC) {
        case '1':
            //UpSvr.RealPlay("34020000001110000001","34020000001110000001","192.168.0.13", 111,"",0,  [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
            //    MkDebugLog("real play return error code:%d\n", ErrorCode);
            //    const CMkStateGridBMediaResponse* pRes = dynamic_cast<const CMkStateGridBMediaResponse*>(pResponse);
            //    if (pRes) {
            //        RealplaySession = pRes->GetSessionId();
            //    }
            //});
            break;
        case '2':
            //UpSvr.Broadcast("34020000001110000001","34020000001110000001", "192.168.0.13", 222, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
            //    const CMkStateGridBMediaResponse* pRes = dynamic_cast<const CMkStateGridBMediaResponse*>(pResponse);
            //    if (pRes)
            //    {
            //        BroadcastSession = pRes->GetSessionId();
            //    }
            //    MkDebugLog("broadcast return error code:%d\n", ErrorCode);
            //});
            break;
        case '3':
            break;
        case '4':
            //UpSvr.PtzControl("34020000001110000001", "34020000001110000001", PtzDownStop, 1, 1, 1,[](Uint32 ErrorCode, const CMkStateGridBResponseBase*) {
            //    MkDebugLog("ptz control return error code:%d\n", ErrorCode);
            //});
            break;
        case '5':
            UpSvr.EventSubscribe("34020000001110000001", "34020000001110000001", 3600, MkStateGridBSubscribeAlarm, MapSubId, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
                const CMkStateGridBSubscribeResponse *pRes = dynamic_cast<const CMkStateGridBSubscribeResponse*>(pResponse);
                if (pRes) {
                    AlarmSubscribeId = pRes->GetSubscribeId();
                }
                MkDebugLog("alarm subscribe return error code:%d\n", ErrorCode);
            });
            break;
        case '6':
            UpSvr.EventSubscribe("34020000001110000001", "34020000001110000001", 3600, MkStateGridBSubscribeStatus, MapSubId, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
                const CMkStateGridBSubscribeResponse *pRes = dynamic_cast<const CMkStateGridBSubscribeResponse*>(pResponse);
                if (pRes) {
                    StatusSubscribeId = pRes->GetSubscribeId();
                }
                MkDebugLog("alarm subscribe return error code:%d\n", ErrorCode);
            });
            break;
        case '7':
            break;
        case '8':
            break;
        case '9':
            //UpSvr.StopRealPlay(RealplaySession,[](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
            //    MkDebugLog("stop real play return\n",ErrorCode);
            //});
            break;
        case 'a':
            //UpSvr.StopBrodcast(BroadcastSession, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
            //    MkDebugLog("stop broad cast return:%d\n",ErrorCode);
            //});
            break;
        case 'b':
            UpSvr.StopEventSubscirbe(AlarmSubscribeId, MkStateGridBSubscribeAlarm, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
                MkDebugLog("stop alarm subscribe return:%d\n", ErrorCode);
            });
            break;
        case 'c':
            UpSvr.StopEventSubscirbe(StatusSubscribeId, MkStateGridBSubscribeStatus, [](Uint32 ErrorCode, const CMkStateGridBResponseBase* pResponse) {
            });
        case 'q':
            bQuit = TRUE;
            break;
        default:
            break;
        }
        if (bQuit) {
            break;
        }
    }
    g_Timer.StopTimerThread();
    Pool.Stop();
    UpSvr.Stop();
#ifdef _WIN32
    WSACleanup();
#endif
    printf("end\n");
    return 0;
}


